<template>
	<canvas canvas-id="canvasArea" id="canvasArea" class="charts" @touchstart="touchArea"></canvas>
</template>

<script>
	import uCharts from '../../js_sdk/u-charts/u-charts/u-charts.js';
	var _self;
	var canvaArea=null;
   
	export default {
		data() {
			return {
				cWidth:'',
				cHeight:'',
				pixelRatio:1,
			}
		},
		props:{
			chartData:{
				type:Object
			}
		},
		watch:{
			chartData(){
				this.getServerData();
				console.log('监听到')
			}
		},
		created() {
			_self = this;
			this.cWidth=uni.upx2px(750);
			this.cHeight=uni.upx2px(180*2);
		},
		methods: {
			getServerData(){
				let Area={categories:[],series:[]};
				//这里我后台返回的是数组，所以用等于，如果您后台返回的是单条数据，需要push进去
				Area.categories= this.chartData.x || ['0FEB', '1FEB', '2FEB', '3FEB', '4FEB', '5FEB', '6FEB', '7FEB', '8FEB', '9FEB', '10FEB', '11FEB', '12FEB', '13FEB', '14FEB'];
				Area.series= [
					{
						name: '',
						data: this.chartData.y || [1600, 1800, 2000,2200, 2240, 2200,2120, 1980, 1700,1600, 1680, 1700,1680, 1400, 1300],
						color: '#69DA73'
					}
				];
				_self.showArea("canvasArea",Area);
			},
			showArea(canvasId,chartData){
				canvaArea=new uCharts({
					$this:_self,
					canvasId: canvasId,
					type: 'area',
					fontSize:11,
					legend:{show:false},
					dataLabel:false,
					dataPointShape:true,
					background:'#202020',
					pixelRatio:_self.pixelRatio,
					categories: chartData.categories,
					series: chartData.series,
					animation: true,
					dataPointShapeType: 'hollow',
					xAxis: {
						type:'grid',
						gridColor:'rgba(255,255,255,0.6)',
						gridType:'dash',
						itemCount: 3,
						dashLength:1000,
						labelCount:3,
					},
					yAxis: {
						gridType:'dash',
						gridColor:'rgba(255,255,255,0.6)',
						dashLength:8,
						splitNumber:5,
						min:10,
						max:180,
					},
					width: _self.cWidth*_self.pixelRatio,
					height: _self.cHeight*_self.pixelRatio,
					extra: {
						area:{
							type: 'curve',
							opacity:0.9,
							addLine:true,
							width:2,
							gradient: true,
						}
					}
				});
				
			},
			touchArea(e) {
				canvaArea.showToolTip(e, {
					format: function (item, category) {
						return category + ' ' + item.name + ':' + item.data 
					}
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	/*样式的width和height一定要与定义的cWidth和cHeight相对应*/
	.qiun-charts {
		width: 750upx;
		height: 180*2upx;
		background-color: #202020;
	}
	
	.charts {
		width: 750upx;
		height: 180*2upx;
		background-color: #202020;
	}
</style>
